<?php

namespace app\common\model;

use common\BaseModel;
use common\helper\DateHelper;
use common\Token\Token;
use common\traits\InstanceTrait;

;


/**
 * Class Record
 * @package app\api\model
 */
class Record extends BaseModel
{

    use InstanceTrait;
    protected $pk = 'record_id';
    protected $type_map = [
        1 => ['color' => 'blue', 'type_name' => '知识提升'],
        2 => ['color' => 'green', 'type_name' => '身体提升'],
        3 => ['color' => 'yellow', 'type_name' => '强迫工作'],
        4 => ['color' => 'orange', 'type_name' => '尽情娱乐'],
        5 => ['color' => 'red', 'type_name' => '浪费拖延'],
    ];

    public function getByMember($member_id)
    {
        return $this->where('member_id', $member_id)->select();
    }

    public function getList($get)
    {
        $member_info = Token::instance()->getUserInfo();
        $field = [
            "record_id",
            "content",
            "exe_time",
            'from_unixtime(exe_time)' => "exe_date",
            "type",
            "member_id",
        ];
        $where = [
            ['member_id', '=', $member_info['member_id']],
        ];
        if (!empty($get['exe_date'])) {
            $where[] = ['exe_time', 'between', DateHelper::to_time(DateHelper::day_cycle($get['exe_date']))];
        }
        if (!empty($get['exe_time'])) {
            $where[] = ['exe_time', 'between', DateHelper::to_time(DateHelper::day_cycle($get['exe_time']))];
        }
        $res = $this->where($where)
                    ->field($field)
                    ->select();
        return $res;
    }

    /**
     * @param $where
     * @return array|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOne($where)
    {

        $res = $this->where($where)
                    ->find();
        return $res;
    }


}
